package com.frxuexi.myroom.repository

import com.frxuexi.lib_base.room.Leave
import com.frxuexi.lib_base.room.LeaveDao
import com.frxuexi.lib_base.room.User
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.withContext
import javax.inject.Inject

@OptIn
class LeaveRepository @Inject constructor(private val leaveDao: LeaveDao) {

    fun addLeave(leave: Leave):Flow<Long>{
        return flow { emit(leaveDao.addLeave(leave)) }
    }

    suspend fun getMyLeaves(uname:String,page:Int,size:Int):List<Leave>{
        return withContext(Dispatchers.IO){
            leaveDao.getMyLeaves(uname, page, size)
        }
    }

    suspend fun getMyCount(uname:String):Int{
        return withContext(Dispatchers.IO){
            leaveDao.getMyCont(uname)
        }
    }

    fun getAllEmp():Flow<List<Leave>>{
        return flow { emit(leaveDao.getAllEmp()) }
    }

    fun approveLeave(leave: Leave):Flow<Int>{
        return flow { emit(leaveDao.approveLeave(leave = leave)) }
    }


}